package lt.vin.currency.server;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import lt.vin.currency.shared.Source;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.google.inject.Singleton;

@Singleton
public class ParserServlet extends HttpServlet  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String xslt = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:string=\"http://exslt.org/strings\"><xsl:template ma" +
			"tch=\"/\"><profile xmlns:tns=\"ht" +
					"tp://www.couchcrowler.com/Profile\" xml" +
					"ns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schem" +
			"aLocation=\"http://www.couchcrowler.com/Profile Profile.xsd\"><tns:id><xsl:value-of select=\"string:tokenize(//td[@class='botnav_15']/a/@href, '=')[2]\"></xsl:value-of></tns:id><tn" +
			"s:name><xsl:value-of select=\"//h1[@class='profile']\" /></tns:name><tns:localtion><tns:longitude><xsl:value-of select=\"string:tokenize(translate(//a[@target='mapquest'], 'Laton: ', ''), ',')[2]\" /></tns:longitude><tns:latitude><xsl:value-of select=\"string:tokenize(translate(//a[@target='mapquest'], 'Laton: ', ''), ',')[1]\" /></tns:latitude></tns:localtion><tns:picture><xsl:value-of select=\"concat(concat(substring-before(//div[@align='center']/a/img/@src, '_m_'), '_t_'), substring-after(//div[@align='center']/a/img/@src, '_m_'))\" /></tns:picture><tns:contacts><xsl:for-each select=\"//td[@class='friends']\"><xsl:if test=\"./a/img/@src\"><tns:id><xsl:value-of select=\"string:tokenize(./a/img/@src, '/')[3]\" /></tns:id></xsl:if></xsl:for-each></tns:contacts></profile></xsl:template></xsl:stylesheet>";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		System.out.println(new File("as").getAbsolutePath());
//		Source source = new Source("coutch", getSource(req), IOUtils.toString(new FileInputStream("coutch.xsl")));
//		Source source = new Source("coutch", getSource(req), IOUtils.toString(getClass().getResourceAsStream("coutch.xsl")));
		Source source = new Source("coutch", getSource(req), xslt);
		
		try {
			resp.setContentType("text/xml");
			resp.getWriter().write(new SourceReader(source).getDataXml());
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
			throw new ServletException(e);
		} catch (TransformerException e) {
			e.printStackTrace();
			throw new ServletException(e);
		} 
	}
	
	private String getSource(HttpServletRequest request) {
		if (StringUtils.isNotBlank(request.getParameter("source"))) {
			return request.getParameter("source");              
		} else if (StringUtils.isNotBlank(request.getParameter("sourceBase64"))) {
			return new String(Base64.decodeBase64(request.getParameter("sourceBase64").getBytes()));
		} else {
			return null;
		}
	}
}
